package com.lx.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lx.dao.ApplicantDao;
import com.lx.dao.ResumeDao;
import com.lx.entity.Resume;
import com.lx.utils.CookieEncrypTool;

@WebServlet("/login.do")
public class ApplicantServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub		
		

		PrintWriter out = resp.getWriter();
		String email = req.getParameter("email");
		String passworrd = req.getParameter("password");
		String remenber = req.getParameter("checkere");
		ApplicantDao appdao = new ApplicantDao();
		ResumeDao redao = new ResumeDao();
		HttpSession session = req.getSession();
		//String sessionId = session.getId();		
		
		int id = appdao.login(email, passworrd);		
		if(id!=0){
			session.setAttribute("username", email);
			session.setAttribute("applicant_id", id);
			
			rememberme(email,passworrd,remenber,req,resp);			
			Resume resumes = redao.getApplicantID(email);
			if(resumes.getBasicinfo_id()==0){
				resp.sendRedirect("applicant/loginGuide.jsp");
			}else{
				session.setAttribute("basecinfoid", resumes.getBasicinfo_id());
				req.setAttribute("resumes", resumes);
				req.getRequestDispatcher("index.jsp").forward(req, resp);
			}
			
		}else{
			out.print("<script type='text/javascript'>");
			out.print("alert('邮箱或者密码错误');");
			out.print("window.location.href='login.jsp';");
			out.print("</script>");
//			resp.sendRedirect("login.jsp");
		}
	}
	
	private void rememberme(String email, String password, String remenber, HttpServletRequest req,
			HttpServletResponse resp) {
		// TODO Auto-generated method stub
		
		if("true".equals(remenber)){
			
			Cookie cookieemail = new Cookie("Cookie_rememberemail",CookieEncrypTool.encdeBase64(email));
			cookieemail.setPath("/");
			cookieemail.setMaxAge(7*24*3600);
			resp.addCookie(cookieemail);
			
			Cookie cookiepwd = new Cookie("Cookie_rememberpwdl",CookieEncrypTool.encdeBase64(password));
			cookiepwd.setPath("/");
			cookiepwd.setMaxAge(7*24*3600);
			resp.addCookie(cookiepwd);
			
		}else{
			
			Cookie[] cookie = req.getCookies();
			if(cookie!=null){
				for(Cookie cookielist:cookie){
					if("Cookie_rememberemail".equals(cookielist.getName()) || "Cookie_rememberpwdl".equals(cookielist.getName())){
						cookielist.setMaxAge(0);
						cookielist.setPath("/");
						resp.addCookie(cookielist);
					}
				}
			}
		}
		
	}

}
